<?php

namespace app\api\controller\v1;

use think\Db;
use think\facade\Validate;
use think\Request;

class Roles extends Comment
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list = Db::name('permission')
            ->alias('p')
            ->join('permission_api pa','p.ps_id = pa.ps_id')
            ->field('p.ps_id as id,p.ps_name as authName,p.ps_pid as pid,pa.ps_api_path as path')
            ->select();

        $roles = Db::name('role')
            ->field('role_id,ps_ids,role_name as roleName,role_desc as roleDesc')
            ->select();

        return $this->getRoles($list,$roles);
    }

    /**
     * 添加角色
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = Request()->param();

        if(empty($data['roleName']) || empty($data['roleDesc'])){
            return $this->out([],'参数不能为空',422);
        }

        $user = [
            'role_name' => $data['roleName'],
            'role_desc' => $data['roleDesc']
        ];

        $id = Db::name('role')->insertGetId($user);

        if(empty($id)){
            return $this->out([],'添加失败',400);
        }

        $res = Db::name('role')
            ->where('id',$id)
            ->field('role_id as roleId,role_name as roleName,role_desc as RoleDesc')
            ->find();

        return $this->out($res,'添加成功',200);
    }

    /**
     * 根据 ID 查询角色
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if(!Validate::isInteger($id)){
            return $this->out([],'id参数错误',422);
        }

        $res = Db::name('role')
            ->where('role_id',$id)
            ->field('role_id as roleId,role_name as roleName,role_desc as roleDesc')
            ->find();

        if(empty($res)){
            return $this->out([],'获取失败',400);
        }

        return $this->out($res,'获取成功',200);
    }

    /**
     * 编辑提交角色
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if(!Validate::isInteger($id)){
            return $this->out([],'id参数错误',422); 
        }

        $data = Request()->param();

        if(empty($data['roleName']) === true){
            return $this->out([],'角色名不能为空',422);
        }

        if(empty($data['roleDesc']) === true){
            $res = Db::name('role')
                ->where('role_id',$id)
                ->update(['role_name' => $data['roleName']]);
        }

        if(empty($data['roleDesc']) === false && empty($data['roleName']) === false){
            $res = Db::name('role')
                ->where('role_id',$id)
                ->update(['role_name' => $data['roleName'],'role_desc'=>$data['roleDesc']]);
        }


        if(empty($res)){
            return $this->out([],'获取失败',400);
        }

        $user = Db::name('role')
            ->where('role_id',$id)
            ->field('role_id as roleId,role_name as roleName,role_desc as roleDesc')
            ->find();

        if(empty($user)){
            return $this->out([],'获取失败',400);
        }

        return $this->out($user,'获取成功',200);

    }

    /**
     * 删除角色
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!Validate::isInteger($id)){
            return $this->out([],'id参数错误',422);
        }

        $res = Db::name('role')->delete($id);

        if(empty($res) === true){
            return $this->out([],'删除失败',400);
        }

        return $this->out([],'删除成功',200);
    }

    //角色授权
    public function rights(Request $request,$roleId)
    {
        if(!Validate::isInteger($roleId)){
            return $this->out([],'id参数错误',422);
        }

        $data = Request()->param();

        $res = Db::name('role')
            ->where('role_id',$roleId)
            ->update(['ps_ids' => $data['rids']]);

        if(empty($res)){
            return $this->out([],'更新失败',400);
        }

        return $this->out([],'更新成功',200);
    }
}
